package com.xci.platform.auth.controller;

import com.xci.platform.annotation.Authorize;
import com.xci.platform.auth.entity.DepartmentEntity;
import com.xci.platform.auth.service.DepartmentService;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.core.Constant;
import com.xci.platform.core.TreeNode;
import com.xci.platform.core.TreeNodeIndex;
import com.xci.platform.helper.ExcelHelper;
import com.xci.platform.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 系统组织机构控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/auth/department")
public class DepartmentController extends AuthBaseController {
    /**
     * 系统部门服务
     */
    @Resource
    protected DepartmentService departmentService;

    //region 页面视图

    /**
     * 组织机构首页
     */
    @GetMapping
    @Authorize(code = "SystemDepartment")
    public String index() {
        return "auth/department/index";
    }

    /**
     * 组织机构新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "SystemDepartment.Create")
    public String create(String parentId, ModelMap map) {
        createMark(map);
        DepartmentEntity entity = new DepartmentEntity();
        entity.setId(StringHelper.guid());
        entity.setParentId(parentId);
        entity.setStatus(1);
        map.put("entity", entity);
        map.put("parentName", getParentName(parentId));
        return "auth/department/edit";
    }

    /**
     * 组织机构编辑页面
     */
    @GetMapping("/edit")
    @Authorize(code = "SystemDepartment.Edit")
    public String edit(String id, ModelMap map) {
        DepartmentEntity entity = departmentService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        map.put("parentName", getParentName(entity.getParentId()));
        return "auth/department/edit";
    }

    private String getParentName(String id) {
        String parentName = Constant.Empty;
        if (StringHelper.isNotBlank(id) && !id.equals(Constant.RootNodeId)) {
            parentName = departmentService.queryNameById(id);
        }
        if (StringHelper.isBlank(parentName)) {
            parentName = "顶层组织机构";
        }
        return parentName;
    }

    /**
     * 组织机构详细页面
     *
     * @param id 组织机构主键
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        DepartmentEntity entity = departmentService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/department/details";
    }

    //endregion

    //region 数据处理

    /**
     * 组织机构Tree
     */
    @ResponseBody
    @GetMapping("/tree")
    public Object tree(@RequestParam Map<String, String> params) {
        params.put("status", "1");
        List<DepartmentEntity> list = departmentService.query(params);
        List<TreeNode> models = departmentService.convertToNodeList(list);
        return BoolMessage.tree(models);
    }

    /**
     * 组织机构表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemDepartment.Search")
    public Object grid(@RequestParam Map params) {
        return BoolMessage.grid(departmentService.query(params));
    }
    /**
     * 保存组织机构
     */
    @ResponseBody
    @PostMapping("/create-save")
    @Authorize(code = "SystemDepartment.Create")
    public BoolMessage createSave(@ModelAttribute DepartmentEntity entity) {
        return departmentService.save(entity, true);
    }

    /**
     * 保存组织机构
     */
    @ResponseBody
    @PostMapping("/edit-save")
    @Authorize(code = "SystemDepartment.Edit")
    public BoolMessage editSave(@ModelAttribute DepartmentEntity entity) {
        return departmentService.save(entity, false);
    }

    /**
     * 保存拖拽数据(父节点和路径)
     */
    @ResponseBody
    @PostMapping("/dnd")
    @Authorize(code = "SystemDepartment.Drag")
    public BoolMessage dnd(@RequestBody TreeNodeIndex[] nodeIndexs) {
        return departmentService.updateNodeIndex(nodeIndexs);
    }

    /**
     * 修改组织机构启用状态
     */
    @ResponseBody
    @PostMapping("/status")
    @Authorize(code = "SystemDepartment.Status")
    public BoolMessage status(String id, Integer status) {
        return departmentService.updateStatus(id, status);
    }

    /**
     * 删除组织机构
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "SystemDepartment.Delete")
    public BoolMessage delete(String id) {
        return departmentService.delete(id, currentUser());
    }

    /**
     * 导出组织机构
     */
    @GetMapping("/export")
    @Authorize(code = "SystemDepartment.Export")
    public void export(@RequestParam Map params, HttpServletResponse response) throws IOException {
        ExcelHelper.exportWeb(response, departmentService.query(params), DepartmentEntity.class, "系统组织机构");
    }

    //endregion
}
